/*=========================================================================
 
Program:   Medical Imaging & Interaction Toolkit
Language:  C++
Date:      $Date: 2010-06-19 02:12:43 +0800 (周六, 19 六月 2010) $
Version:   $Revision: 23881 $
 
Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.
 
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.
 
=========================================================================*/

#include "WxContourToolOverlay.h"

#include <mitkContour.h>

//#include "ipSegmentation.h"
//#include "mitkDataNode.h"
#include <mitkProperties.h>
#include <mitkStringProperty.h>
#include <mitkPointOperation.h>
#include <mitkAction.h>
#include <mitkPositionEvent.h>
#include <mitkInteractionConst.h>
#include <mitkPositionEvent.h>
#include <mitkStateEvent.h>
#include <mitkVtkPropRenderer.h>


#include <vtkRenderWindowInteractor.h>
mitk::WxContourToolOverlay::WxContourToolOverlay(const char * type, mitk::DataNode* dataNode)
    : mitk::StateMachine(type), m_Started(false),m_ContourOnVtkRenderer(NULL)
{
  m_DataNode=dataNode;
  assert(m_DataNode.IsNotNull());

  m_DataNode->SetProperty("layer", mitk::IntProperty::New(1000) );
  m_DataNode->SetProperty("name", mitk::StringProperty::New("InteractiveFeedbackData") );
  m_DataNode->SetOpacity(1);
  m_DataNode->SetColor(0.4,0.9,0.0);
  m_DataNode->SetProperty( "Width", mitk::FloatProperty::New(1.0) );
}

mitk::WxContourToolOverlay::~WxContourToolOverlay()
{

}


bool mitk::WxContourToolOverlay::ExecuteAction(mitk::Action* action, mitk::StateEvent const* stateEvent)
{
  mitk::Point3D eventPoint;
  const mitk::DisplayPositionEvent* displayPosEvent = dynamic_cast<const mitk::DisplayPositionEvent*>(stateEvent->GetEvent());
  mitk::VtkPropRenderer* sender = (mitk::VtkPropRenderer*) stateEvent->GetEvent()->GetSender();
  if((displayPosEvent == NULL) || (sender == NULL))
    return false;

  eventPoint[0] = displayPosEvent->GetDisplayPosition()[0];
  eventPoint[1] = displayPosEvent->GetDisplayPosition()[1];
  eventPoint[2] = 0;


  bool ok = false;
  switch (action->GetActionId())
  {
  case mitk::AcNEWPOINT:
    {

          vtkRenderWindow *renderWindow = NULL;
          vtkRenderWindowInteractor *renderWindowInteractor = NULL;

          mitk::VtkPropRenderer* renderer = (mitk::VtkPropRenderer*) stateEvent->GetEvent()->GetSender();

          if ( renderer != NULL&&renderer->GetMapperID()==mitk::BaseRenderer::Standard3D )
          {
              renderWindow = renderer->GetRenderWindow();
              if ( renderWindow != NULL )
              {
                  renderWindowInteractor = renderWindow->GetInteractor();
              }

              renderWindowInteractor->Disable();
              this->m_DataNode->SetVisibility(false);
              this->m_DataNode->SetVisibility(true,renderer);
              Press(eventPoint);
              ok = true;
              m_Started = true;
          }
      break;
    }
  case mitk::AcINITMOVEMENT:
    {
      if (m_Started)
      {
        Move(eventPoint);
        ok = true;
        break;
      }
    }
  case mitk::AcMOVEPOINT:
    {
      if (m_Started)
      {
        mitk::VtkPropRenderer* renderer = (mitk::VtkPropRenderer*) stateEvent->GetEvent()->GetSender();
        Move(eventPoint);
        mitk::RenderingManager::GetInstance()->ForceImmediateUpdate(renderer->GetVtkRenderer()->GetRenderWindow());
        ok = true;
        break;
      }
    }
  case mitk::AcFINISHMOVEMENT:
    {
      if (m_Started)
      {
          mitk::VtkPropRenderer* renderer = (mitk::VtkPropRenderer*) stateEvent->GetEvent()->GetSender();
          vtkRenderWindow *renderWindow = NULL;
          vtkRenderWindowInteractor *renderWindowInteractor = NULL;

          if ( renderer != NULL )
          {
            renderWindow = renderer->GetRenderWindow();
            if ( renderWindow != NULL )
            {
              renderWindowInteractor = renderWindow->GetInteractor();
            }
          }
          renderWindowInteractor->Enable();
          this->m_ContourOnVtkRenderer=renderer->GetVtkRenderer();
        ok = true;
        m_Started = false;
      }
      break;
    }
  default:
    ok = false;
    break;
  }
  return ok;
}

void mitk::WxContourToolOverlay::Press(mitk::Point3D& point)
{
  mitk::Contour* contour = dynamic_cast<mitk::Contour*>(m_DataNode->GetData());
  assert(contour!=NULL);

  if (!m_Positive)
    m_DataNode->SetColor(1.0,0.0,0.0);

  contour->Initialize();
  contour->AddVertex( point );
}

void mitk::WxContourToolOverlay::Move(mitk::Point3D& point)
{
  mitk::Contour* contour = dynamic_cast<mitk::Contour*>(m_DataNode->GetData());
  assert(contour!=NULL);

  contour->AddVertex( point );

//  m_Parent->UpdateWidgets();
}

void mitk::WxContourToolOverlay::Release(mitk::Point3D& /*point*/)
{

}
